alert('SceneEnigmaBalls.js loaded');

var EB_SelectionStep;
var EB_ChoiceP;
var EB_ChoiceB;
var EB_VerrP1;
var EB_VerrP2;
var EB_VerrP3;
var EB_VerrB1;
var EB_VerrB2;
var EB_VerrB3;
var EB_End;


function SceneEnigmaBalls() {

};

SceneEnigmaBalls.prototype.initialize = function () {
	alert("SceneEnigmaBalls.initialize()");
	// this function will be called only once when the scene manager show this scene first time
	// initialize the scene controls and styles, and initialize your variables here
	// scene HTML and CSS will be loaded before this function is called
	
	sf.scene.show('ToolBar');
	
	$('#TextEnigmaBalls').css('left',ExtractX(4,1));
	$('#TextEnigmaBalls').css('top',ExtractY(4,1));
	$('#TextEnigmaBalls').css('margin-top','-30px');
	
	$('#TextEBJ1').css('left',ExtractX(0,2));
	$('#TextEBJ1').css('top',ExtractY(0,2));
	
	$('#TextEBJ2').css('left',ExtractX(0,4));
	$('#TextEBJ2').css('top',ExtractY(0,4));

	
	$('#TextEBJ3').css('left',ExtractX(0,6));
	$('#TextEBJ3').css('top',ExtractY(0,6));
	
	EB_init();

};

SceneEnigmaBalls.prototype.handleShow = function (data) {
	alert("SceneEnigmaBalls.handleShow()");
	// this function will be called when the scene manager show this scene
	EB_init();
};

SceneEnigmaBalls.prototype.handleHide = function () {
	alert("SceneEnigmaBalls.handleHide()");
	// this function will be called when the scene manager hide this scene
	scene = "";
};

SceneEnigmaBalls.prototype.handleFocus = function () {
	alert("SceneEnigmaBalls.handleFocus()");
	// this function will be called when the scene manager focus this scene
};

SceneEnigmaBalls.prototype.handleBlur = function () {
	alert("SceneEnigmaBalls.handleBlur()");
	// this function will be called when the scene manager move focus to another scene from this scene
};

SceneEnigmaBalls.prototype.handleKeyDown = function (keyCode) {
	alert("SceneEnigmaBalls.handleKeyDown(" + keyCode + ")");
	// TODO : write an key event handler when this scene get focued
	switch (keyCode) {
		case sf.key.LEFT:
			break;
		case sf.key.RIGHT:
			break;
		case sf.key.UP:
			break;
		case sf.key.DOWN:
			break;
		case sf.key.ENTER:
			if (EB_SelectionStep != 2)
				EB_HandleSelect(9);
			else{
				//Scene Suivante;
				alert("VIIIIIIIIIIIIIIICTOIIIIIIIIIIIIIIIIIIIIIIIRE");
			}
			break;
		case sf.key.RETURN:
			event.preventDefault();
			sf.scene.hide('EnigmaBalls');
			sf.scene.show('Scene2');
			sf.scene.focus('Scene2');
			break;
		case sf.key.N1:
			EB_HandleSelect(101);
			break;			
		case sf.key.N2:
			EB_HandleSelect(98);
			break;			
		case sf.key.N3:
			EB_HandleSelect(6);
			break;
		default:
			alert("handle default key event, key code(" + keyCode + ")");
			break;
	}
};

function EB_init(){
	$('#EB_SelectPlayer').css('visibility','hidden');
	$('#EB_SelectBall').css('visibility','hidden');
	
	$('#EB_SelectedP1').css('visibility','hidden');
	$('#EB_SelectedP2').css('visibility','hidden');
	$('#EB_SelectedP3').css('visibility','hidden');
	$('#EB_SelectedB1').css('visibility','hidden');
	$('#EB_SelectedB2').css('visibility','hidden');
	$('#EB_SelectedB3').css('visibility','hidden');
	
	$('#EB_SelectedP1').css('left',ExtractX(0,2));
	$('#EB_SelectedP1').css('top',ExtractY(0,2));
	$('#EB_SelectedP1').css('margin-left','0px');
	
	$('#EB_SelectedP2').css('left',ExtractX(0,4));
	$('#EB_SelectedP2').css('top',ExtractY(0,4));
	$('#EB_SelectedP2').css('margin-left','20px');
	
	$('#EB_SelectedP3').css('left',ExtractX(0,6));
	$('#EB_SelectedP3').css('top',ExtractY(0,6));
	$('#EB_SelectedP3').css('margin-left','40px');
	
	$('#EB_SelectedB1').css('left',ExtractX(2,2));
	$('#EB_SelectedB1').css('top',ExtractY(2,2));
	$('#EB_SelectedB1').css('margin-left','-10px');
	$('#EB_SelectedB1').css('margin-top','-20px');
	
	$('#EB_SelectedB2').css('left',ExtractX(2,4));
	$('#EB_SelectedB2').css('top',ExtractY(2,4));
	$('#EB_SelectedB2').css('margin-left','16px');
	$('#EB_SelectedB2').css('margin-top','-18px');
	
	$('#EB_SelectedB3').css('left',ExtractX(2,6));
	$('#EB_SelectedB3').css('top',ExtractY(2,6));
	$('#EB_SelectedB3').css('margin-left','37px');
	$('#EB_SelectedB3').css('margin-top','-10px');
	
	EB_SelectionStep = 0;
	EB_ChoiceP = 0;
	EB_ChoiceB = 0;
	
	EB_VerrP1 == false;
	EB_VerrP2 == false;
	EB_VerrP3 == false;
	EB_VerrB1 == false;
	EB_VerrB2 == false;
	EB_VerrB3 == false;

	EB_End = 3;
}

function EB_HandleSelect(key) {
	
	alert("--------------- EB_HandleSelect ---------------------");
	if (key == 9){
		
		alert("--------------- HandleKey/key.ENTER ---------------------");
		if (EB_SelectionStep == 0 && EB_ChoiceP != 0){
			alert("--------------- HandleKey/key.ENTER Make player border green ---------------------");
			$('#EB_SelectPlayer').css('border','5px solid green');
			EB_SelectionStep = 1;
		}else if(EB_SelectionStep == 1 && EB_ChoiceB != 0){
			alert("--------------- HandleKey/key.ENTER Make ball border green ---------------------");
			$('#EB_SelectBall').css('border','5px solid green');
			if(EB_ChoiceP == 1 && EB_ChoiceB == 2){
				alert("--------------- HandleKey/key.ENTER verr P1-B2 ---------------------");
				EB_VerrP1 = true;
				EB_VerrB2 = true;
				EB_End--;
				alert("/////// "+EB_End);
				$('#EB_SelectedP1').css('visibility','visible');
				$('#EB_SelectedB2').css('visibility','visible');
				$('#EB_SelectPlayer').css('visibility','hidden');
				$('#EB_SelectBall').css('visibility','hidden');
				$('#EB_SelectBall').css('border','5px solid white');
				$('#EB_SelectPlayer').css('border','5px solid white');
				if (EB_End !=0){
					EB_SelectionStep = 0;
					EB_ChoiceP = 0;
					EB_ChoiceB =0;
				}else{
					//AddPoint(6);
					EB_SelectionStep = 2;
				}
			}else if (EB_ChoiceP == 2 && EB_ChoiceB == 3){
				alert("--------------- HandleKey/key.ENTER verr P2-B3 ---------------------");
				EB_VerrP2 = true;
				EB_VerrB3 = true;
				EB_End--;
				alert("/////// "+EB_End);
				$('#EB_SelectedP2').css('visibility','visible');
				$('#EB_SelectedB3').css('visibility','visible');
				$('#EB_SelectPlayer').css('visibility','hidden');
				$('#EB_SelectBall').css('visibility','hidden');
				$('#EB_SelectBall').css('border','5px solid white');
				$('#EB_SelectPlayer').css('border','5px solid white');
				if (EB_End !=0){
					EB_SelectionStep = 0;
					EB_ChoiceP = 0;
					EB_ChoiceB =0;
				}else{
					//AddPoint(6);
					EB_SelectionStep = 2;
				}
			}else if (EB_ChoiceP == 3 && EB_ChoiceB == 1){
				alert("--------------- HandleKey/key.ENTER verr P3-B1 ---------------------");
				EB_VerrP3 = true;
				EB_VerrB1 = true;
				EB_End--;
				alert("/////// "+EB_End);
				$('#EB_SelectedP3').css('visibility','visible');
				$('#EB_SelectedB1').css('visibility','visible');
				$('#EB_SelectPlayer').css('visibility','hidden');
				$('#EB_SelectBall').css('visibility','hidden');
				$('#EB_SelectBall').css('border','5px solid white');
				$('#EB_SelectPlayer').css('border','5px solid white');
				if (EB_End !=0){
					EB_SelectionStep = 0;
					EB_ChoiceP = 0;
					EB_ChoiceB =0;
				}else{
					//AddPoint(6);
					EB_SelectionStep = 2;
				}
			}else{
				alert("--------------- HandleKey/key.ENTER Reinit ---------------------");
				$('#EB_SelectBall').css('border','5px solid white');
				$('#EB_SelectPlayer').css('border','5px solid white');
				$('#EB_SelectPlayer').css('visibility','hidden');
				$('#EB_SelectBall').css('visibility','hidden');
				EB_SelectionStep = 0;
				EB_ChoiceP = 0;
				EB_ChoiceB = 0;
			}
			if(EB_End==0){
				alert("------------------------------------------------");
				//AddPoint(6);
				EB_SelectionStep = 2;
			}
		}
		
	}else{
		
		if(EB_SelectionStep == 0){
			$('#EB_SelectPlayer').css('visibility','visible');
			alert("------------------------ ENTER SELECTION PLAYER ---------------------------------");
			switch(key){
				case 101:
					if (!EB_VerrP1){
						alert("///// P1 selected");
						$('#EB_SelectPlayer').css('left',ExtractX(0,2));
						$('#EB_SelectPlayer').css('top',ExtractY(0,2));
						$('#EB_SelectPlayer').css('margin-left','0px');
						EB_ChoiceP = 1;
					}else
						alert("///// P1 aleardy locked !");
					break;
				case 98:
					if (!EB_VerrP2){
						alert("///// P2 selected");
						$('#EB_SelectPlayer').css('left',ExtractX(0,4));
						$('#EB_SelectPlayer').css('top',ExtractY(0,4));
						$('#EB_SelectPlayer').css('margin-left','20px');
						EB_ChoiceP = 2;
					}else
						alert("///// P2 aleardy locked !");
					break;
				case 6:
					if (!EB_VerrP3){
						alert("///// P3 selected");
						$('#EB_SelectPlayer').css('left',ExtractX(0,6));
						$('#EB_SelectPlayer').css('top',ExtractY(0,6));
						$('#EB_SelectPlayer').css('margin-left','40px');
						EB_ChoiceP = 3;
					}	else
						alert("///// P3 aleardy locked !");		
					break;
				default:
					alert("Fuck your kitty");
			}
		}
		if(EB_SelectionStep == 1){
			$('#EB_SelectBall').css('visibility','visible');
			switch(key){
				case 101:
					if (!EB_VerrB1){
						alert("///// B1 selected");
						$('#EB_SelectBall').css('left',ExtractX(2,2));
						$('#EB_SelectBall').css('top',ExtractY(2,2));
						$('#EB_SelectBall').css('margin-left','-10px');
						$('#EB_SelectBall').css('margin-top','-20px');
						EB_ChoiceB = 1;
					}else
						alert("///// B1 aleardy locked !");
					break;
				case 98:
					if (!EB_VerrB2){
						alert("///// B2 selected");
						$('#EB_SelectBall').css('left',ExtractX(2,4));
						$('#EB_SelectBall').css('top',ExtractY(2,4));
						$('#EB_SelectBall').css('margin-left','16px');
						$('#EB_SelectBall').css('margin-top','-18px');
						EB_ChoiceB = 2;
					}else
						alert("///// B2 aleardy locked !");
					break;
				case 6:
					if (!EB_VerrB3){
						alert("///// B3 selected");
						$('#EB_SelectBall').css('left',ExtractX(2,6));
						$('#EB_SelectBall').css('top',ExtractY(2,6));
						$('#EB_SelectBall').css('margin-left','37px');
						$('#EB_SelectBall').css('margin-top','-10px');
						EB_ChoiceB = 3;
					}else
						alert("///// B3 aleardy locked !");
					break;
				default:
					alert("Fuck your kitty");
			}
		}

	}
	
}
